define(function (require,exports,module) {
    var myPub = require('./public');
    var oCommentList = $('.js_commentList');
    var oContain = $('.js_contain');
    var oWriteMsg = $('.js_writeMsg');                      // 写留言按钮
    var oLeaveMsgElm = $('.js_leaveMsg');                   // 留言区域
    var oLeaveMsgClose = $('.js_leaveMsgClose');            // 关闭留言按钮
    var oLeaveMsgBtn = $('.js_leaveMsgBtn');                // 留言按钮
    var oCountNum = $('.js_countNum');                      // 字数
    var oLeaveMsgInputDel = $('.leaveMsgInputDel');         // 留言内容区域
    var oReplyList = $('.js_commentReplyList');             // 当前页面留言区域
    var oEndLine = $('.js_endLine');                        // 结尾的线
    var oLoading = $('.js_loading');                        // 继续加载
    var oLeaveMsg = '';                                     // 留言内容
    var bSubmit = false;                                    // 字数够不够
    var bSubmitTap = true;                                  // 提交按钮防多次点击
    var leaveMsg_timer = null;                              // 留言定时器
    var MsgScrollTap_timer = null;                          // 失去焦点滚动到底部的定时器


    var commentUrl = '/mails/getArticleReplys';             // 评论列表
    var replyUrl = '/mails/replyArticle';                   // 评论接口


    module.exports={
        init:function () {
            comment.articleInit();
            comment.commentData();
            comment.addScroll();
        }
    };

    // 留言区域
    oLeaveMsgInputDel.on('input propertychange',function (e) {
        oLeaveMsg = $(this).val();
        var msgNum = oLeaveMsg.replace(/[\ |\~|\`|\!|\@|\#|\$|\%|\^|\&|\*|\(|\)|\-|\_|\+|\=|\||\\|\[|\]|\{|\}|\；|\：|\《|\》|\，|\。|\？|\！|\“|\”|\;|\:|\"|\'|\,|\<|\.|\>|\/|\?|\n|\r]/g,"");
        if(msgNum.length >= 5){
            bSubmit = true
        }else{
            bSubmit = false
        }
        if(oLeaveMsg == '' ){
            oCountNum.text('5-')
        }else{
            oCountNum.text(oLeaveMsg.length+'/')
        }
    });

    // 失去焦点返回底部
    oLeaveMsgInputDel.blur(function () {
        clearTimeout(MsgScrollTap_timer);
        MsgScrollTap_timer = setTimeout(function () {
            window.scrollTo(0,document.documentElement.clientHeight);
        },100);
    });

    // 点击留言按钮
    oLeaveMsgBtn.on('click',function (e) {
        if(!bSubmitTap)return;
        bSubmitTap = false;
        if(oLeaveMsgInputDel.val() == ''){
            myPub.TipLayer('请填写留言')
        }else if(!bSubmit){
            myPub.TipLayer('留言内容必须不少于5个字（标点符号不计算在内）！')
        }else{
            G_JSON_POST(replyUrl,{
                'articleId':articleId,
                'reply':oLeaveMsgInputDel.val()
            },function (data) {
                var getData = [];
                if(data.status == 'success'){
                    getData = [data.obj];
                    oReplyList.append(comment.commentHtml(getData));
                    myPub.TipLayerEnd('留言成功','no',function () {
                        oLeaveMsgElm.hide().stop().animate({bottom:comment.pageHeight},500);
                        oLeaveMsgInputDel.val('');
                        oCountNum.text('5-');
                    },2)
                }else{
                    myPub.TipLayer(data.message,'no')
                }
            })
        }
        e.stopPropagation();
        clearTimeout(leaveMsg_timer);
        leaveMsg_timer = setTimeout(function () {
            bSubmitTap = true;
        },300)
    });

    var comment = {
        start:0,                    // 开始数据
        limit:20,                   // 每次请求的数据
        getData:[],                 // 数据记录
        pagearr: 1,                 // 当前页码
        scroll_timer : null,        // 滚动加载定时器
        loadOff:false,              // 加载开关
        pageHeight:0,               // 页面高度

        // 页面初始化
        articleInit:function(){
          var _this = this;
          _this.pageHeight = $(document).height();
          oLeaveMsgElm.css({'bottom':-_this.pageHeight});
          _this.msgEvent()
        },

        // 留言区域点击事件
        msgEvent:function(){
            var _this = this;
            // 写留言按钮
            oWriteMsg.on('click',function (e) {
                oLeaveMsgElm.show().animate({ 'bottom':0 },500);
                e.stopPropagation();
            });

            // 关闭留言区域按钮
            oLeaveMsgClose.on('click',function (e) {
                oLeaveMsgElm.hide().stop().animate({bottom:-_this.pageHeight},500);
                e.stopPropagation();
            })
        },

        // 获取留言列表
        commentData:function(){
            var _this = this;
            G_JSON_POST(commentUrl,{
                'articleId':articleId,
                'start':_this.start,
                'limit':_this.limit
            },function (data) {
                if(data.status == 'success'){
                    _this.getData = data.obj;
                    if(_this.getData.length == 0 && _this.pagearr < 2){
                        _this.loadOff = true;
                        oLoading.hide();
                        oEndLine.show();
                        return
                    }
                    if(_this.pagearr >= 1){
                        if(_this.getData.length == 0){
                            _this.loadOff = true;
                            oLoading.hide();
                            oEndLine.show();
                            return
                        }
                    }
                    //拼接页面
                    oCommentList.append(_this.commentHtml(_this.getData));

                    if(_this.getData.length <  _this.limit) {
                        _this.bLoadOff = true;
                        oLoading.hide();
                        oEndLine.show();
                    }

                    data = null;
                }else{
                    oLoading.hide();
                    oEndLine.show();
                    myPub.TipLayer(data.message,'no')
                }
            })
        },

        // 滚动加载
        addScroll:function(){
            var _this = this;
            var minScrHeight = 300;
            oContain.off('scorll');
            oContain.on('scroll',function () {
                var afterScroll = $(this).scrollTop();
                clearTimeout(_this.scroll_timer);
                _this.scroll_timer = setTimeout(function () {
                    if(oContain[0].scrollHeight - afterScroll - oContain.height() < minScrHeight){
                        if(!_this.loadOff){
                            // 滚动底部页数增加 起始数据等于上次结束数据  新的结束等于页数*页码数
                            _this.start = _this.limit*_this.pagearr;
                            _this.pagearr++;
                            _this.commentData();
                            // _this.commentData(_this.start, _this.limit);
                        }
                    }
                },150)
            })
        },

        // 评论html拼接
        commentHtml:function (data) {
            var _this = this;
            var thtml = '';
            for(var i = 0;i<data.length;i++){
                var authorHtml = '';
                var srcHtml = '';

                if(data[i].headImg){
                    if(data[i].headImg.indexOf('http') != -1){
                        srcHtml = data[i].headImg
                    }else{
                        srcHtml = HTTP_IMAGE_URL+data[i].headImg
                    }
                }else {
                    srcHtml = BASE_URL+'/static_v4/image/sfddj.png'
                }

                if(data[i].authorReply && data[i].authorReply != null && data[i].authorReply.length > 0 ){
                    authorHtml =
                        '<p class="commentMsgAuthor commentMsgCus">作者</p>'+
                        '<p class="commentMsgAuthorDel commentMsgCusDel">'+data[i].authorReply+'</p>';
                }
                thtml+=
                    '<li class="HBox">'+
                    '   <div class="commentMsgHead">'+
                    '       <span class="commentMsgHeadImg">'+
                    '           <img src="'+srcHtml+'" alt="">'+
                    '       </span>'+
                    '   </div>'+
                    '   <div class="commentMsg Bflex1">'+
                    '       <p class="commentMsgCus">'+data[i].name+'</p>'+
                    '       <p class="commentMsgCusDel">'+data[i].reply+'</p>'+
                            authorHtml+
                    '   </div>' +
                    '</li>';
            }
            return thtml;
        }
    };
});